package com.bw.module_main


import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.bw.module_cinema.CinemaFragment
import com.bw.module_home.HomeFragment
import com.bw.module_live.LiveFragment
import com.bw.module_mine.MineFragment
import com.bw.module_publish.PublishFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NavigationMainActivity : AppCompatActivity() {
    // 初始化控件（注意：不要初始化为 null，改为 lateinit 延迟初始化）
    private lateinit var viewPager: ViewPager2
    private lateinit var bottomNav: BottomNavigationView

    // 定义“五河模块”的 Fragment 列表
    private val fragments = listOf(

        HomeFragment(), // 首页
        CinemaFragment(),// 数据页
        LiveFragment(), // 消息页

        PublishFragment(),
        MineFragment()  // 我的页

    )
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_navigation_main)

//        FpsMonitor.instance.start(1000)
        viewPager = findViewById(R.id.view_Pager)
        bottomNav = findViewById(R.id.bottomNav)
// 关闭底部导航的标签行为模式（强制文字始终显示）
        bottomNav.labelVisibilityMode = NavigationBarView.LABEL_VISIBILITY_LABELED

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        viewPager.adapter = object : FragmentStateAdapter(this) {

            override fun getItemCount(): Int = fragments.size


            override fun createFragment(position: Int): Fragment = fragments[position]
        }


        bottomNav.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.nav_home -> viewPager.currentItem = 0 // 首页对应位置 0
                R.id.nav_data -> viewPager.currentItem = 1  // 数据页对应位置 1
                R.id.nav_message -> viewPager.currentItem = 2 // 消息页对应位置 2
                R.id.nav_fb -> viewPager.currentItem = 3
                R.id.nav_mine -> viewPager.currentItem = 4   // 我的页对应位置 3
            }
            true // 消费事件
        }


        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)

                when (position) {
                    0 -> bottomNav.selectedItemId = R.id.nav_home
                    1 -> bottomNav.selectedItemId = R.id.nav_data
                    2 -> bottomNav.selectedItemId = R.id.nav_message
                    3 -> bottomNav.selectedItemId = R.id.nav_fb
                    4 -> bottomNav.selectedItemId = R.id.nav_mine
                }
            }
        })


    }

    override fun onDestroy() {
        super.onDestroy()
//        FpsMonitor.instance.stop()
    }


}